<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Chapter 3. Database Records</title>
    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
    <link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
    <link rel="up" href="index.html" title="Getting Started with Berkeley DB" />
    <link rel="prev" href="CoreDbUsage.html" title="Database Example" />
    <link rel="next" href="usingDbt.html" title="Reading and Writing Database Records" />
  </head>
  <body>
    <div xmlns="" class="navheader">
      <div class="libver">
        <p>Library Version 11.2.5.3</p>
      </div>
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">Chapter 3. Database Records</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="CoreDbUsage.html">Prev</a> </td>
          <th width="60%" align="center"> </th>
          <td width="20%" align="right"> <a accesskey="n" href="usingDbt.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="chapter" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title"><a id="DBEntry"></a>Chapter 3. Database Records</h2>
          </div>
        </div>
      </div>
      <div class="toc">
        <p>
          <b>Table of Contents</b>
        </p>
        <dl>
          <dt>
            <span class="sect1">
              <a href="DBEntry.html#usingDbEntry">Using Database Records</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="usingDbt.html">Reading and Writing Database Records</a>
            </span>
          </dt>
          <dd>
            <dl>
              <dt>
                <span class="sect2">
                  <a href="usingDbt.html#databaseWrite">Writing Records to the Database</a>
                </span>
              </dt>
              <dt>
                <span class="sect2">
                  <a href="usingDbt.html#CoreDatabaseRead">Getting Records from the Database</a>
                </span>
              </dt>
              <dt>
                <span class="sect2">
                  <a href="usingDbt.html#recordDelete">Deleting Records</a>
                </span>
              </dt>
              <dt>
                <span class="sect2">
                  <a href="usingDbt.html#datapersist">Data Persistence</a>
                </span>
              </dt>
            </dl>
          </dd>
          <dt>
            <span class="sect1">
              <a href="cstructs.html">Using C Structures with DB</a>
            </span>
          </dt>
          <dd>
            <dl>
              <dt>
                <span class="sect2">
                  <a href="cstructs.html#cstructdynamic">C Structures with Pointers</a>
                </span>
              </dt>
            </dl>
          </dd>
          <dt>
            <span class="sect1">
              <a href="DbUsage.html">Database Usage Example</a>
            </span>
          </dt>
        </dl>
      </div>
      <p>
      DB records contain two parts — a key and some data. Both the key
    and its corresponding data are 
      encapsulated in
           
          <span><code class="classname">DBT</code> structures.</span> 
           
    Therefore, to access a DB record, you need two such
        <span>structures,</span>
         one for the key and
        one for the data.
  </p>
      <p>
    <code class="classname">DBT</code> structures provide a <code class="literal">void *</code>
    field that you use to point to your data, and another field that identifies
    the data length.  They can therefore be used to store anything from simple
    primitive data to complex structures so long as the information you want to
    store resides in a single contiguous block of memory.
  </p>
      <p>
    This chapter describes 
    <code class="classname">DBT</code> 
     
    usage. It also 
    introduces storing and retrieving key/value pairs from a database. 
  </p>
      <div class="sect1" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h2 class="title" style="clear: both"><a id="usingDbEntry"></a>Using Database Records</h2>
            </div>
          </div>
        </div>
        <p>
        Each database record is comprised of two 
        
        <span><code class="classname">DBT</code> structures</span>
        
        — one for the key and another for the data. 

        
    </p>
        <p>
        To store a database record where the key and/or the data are primitive
        data (<code class="literal">int</code>, <code class="literal">float</code>, and so forth),
        or where the key and/or the data contain an array, we need only to point
        to the memory location where that data resides and identify its
        length. For example:
    </p>
        <a id="c_dbt1"></a>
        <pre class="programlisting">#include &lt;db.h&gt;
#include &lt;string.h&gt;

...

DBT key, data;
float money = 122.45;
char *description = "Grocery bill.";

/* Zero out the DBTs before using them. */
memset(&amp;key, 0, sizeof(DBT));
memset(&amp;data, 0, sizeof(DBT));

key.data = &amp;money;
key.size = sizeof(float);

data.data = description;
data.size = strlen(description) + 1; </pre>
        <p>
    To retrieve the record, simply assign the <code class="literal">void *</code> returned in the 
    <code class="methodname">DBT</code>
    
    to the appropriate variable.
</p>
        <p>
    Note that in the following example we do not allow DB to assign the
    memory for the retrieval of the money value. The reason why is that some
    systems may require float values to have a specific alignment, and the
    memory as returned by DB
    may not be properly aligned (the same problem may exist for structures
    on some systems). We tell DB to use our memory instead of its
    own by specifying the <code class="literal">DB_DBT_USERMEM</code> flag. Be aware that
    when we do this, we must also identify how much user memory is available 
    through the use of the <code class="literal">ulen</code> field.
</p>
        <a id="c_dbt2"></a>
        <pre class="programlisting">#include &lt;db.h&gt;
#include &lt;string.h&gt;

...

float money;
DBT key, data;
char *description;

/* Initialize the DBTs */
memset(&amp;key, 0, sizeof(DBT));
memset(&amp;data, 0, sizeof(DBT));

key.data = &amp;money;
key.ulen = sizeof(float);
key.flags = DB_DBT_USERMEM;

/* Database retrieval code goes here */

/* 
 * Money is set into the memory that we supplied.
 */
description = data.data;</pre>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="CoreDbUsage.html">Prev</a> </td>
          <td width="20%" align="center"> </td>
          <td width="40%" align="right"> <a accesskey="n" href="usingDbt.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">Database Example </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> Reading and Writing Database Records</td>
        </tr>
      </table>
    </div>
  </body>
</html>
